$(function () {
    var pendingApprovalDatagrid,insureBillNumber,applicationForms,minDate,maxDate,organizationName,aplicant,product;
    pendingApprovalDatagrid = $("#pending_approval_datagrid");

    pendingApprovalDatagrid.datagrid({
        fit:true,
        pagination:true,
        url:'/approved_policy/list',
        buttons:'#tb',
        columns:[
            [
                {field:'ck',checkbox:true},
                {field:'insureBillNumber',align:'center',width:200,title:'申请单号'},
                {field:'applicant_name',align:'center',width:100,title:'投保人'},
                {field:'inputTime',align:'center',width:180,title:'申请时间'},
                {field:'product_name',align:'center',width:180,title:'产品名称'},
                {field:'engineNum',align:'center',width:200,title:'发动机号'},
                {field:'vin',align:'center',width:120,title:'车架号'},
                {field:'carPlateNumber',align:'center',width:180,title:'车牌号'},
                {field:'organizationName',align:'center',width:180,title:'操作机构'},
                {field:'insureRisk',align:'center',width:180,title:'险别'},
                {field:'status',align:'center',width:180,title:'保单状态',formatter:statusFormatter},
                {field:'holder',align:'center',width:100,title:'任务提交人'},
                {field:'auditor',align:'center',width:100,title:'审核人'}
            ]
        ]
    });


    $("a[data-cmd]").on("click",function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    var cmdObj = {
        reload : function () {
            pendingApprovalDatagrid.datagrid("reload");
        },
        seach : function () {
            insureBillNumber = $(":input[name='insureBillNumber']").val();
            applicationForms = $("input[type='radio']:checked").val();
            minDate = $(":input[name='minDate']").val();
            maxDate = $(":input[name='maxDate']").val();
            organizationName = $(":input[name='organizationName']").val();
            aplicant = $(":input[name='aplicant']").val();
            product =$(":input[name='product']").val();
            pendingApprovalDatagrid.datagrid("load",{
                insureBillNumber:insureBillNumber,
                applicationForms:applicationForms,
                minDate:minDate,
                maxDate:maxDate,
                organizationName:organizationName,
                aplicant:aplicant,
                product:product
            });
        },
        receive:function () {
            var row = pendingApprovalDatagrid.datagrid("getSelected");
            if(row){
                window.location.href="/pending_approval/receive?holderId="+row.holder_id + "&orderBillId=" + row.orderbill;
            }else{
                $.messager.alert("温馨提示","请选择一条需要修改的数据。","warning");
            }
        },
        untread:function () {
            var rowData = pendingApprovalDatagrid.datagrid("getSelected");
            if(rowData){
                $.messager.confirm("温馨提示","您确定需要作废该保单吗?",function(yes){
                    if(yes){
                        $.get("/pending_approval/untread?orderBillId=" + rowData.orderbill,function(data){
                            if(data.success){
                                pendingApprovalDatagrid.datagrid("reload");
                                $.messager.alert("温馨提示",data.msg,"info");
                            }else{
                                $.messager.alert("温馨提示",data.msg,"error");
                            }
                        },"json")
                    }
                });
            }else{
                $.messager.alert("温馨提示","请选择一条需要修改的数据。","warning");
            }
        }
    }

    // var pager = pendingApprovalDatagrid.datagrid().datagrid('getPager');	// get the pager of datagrid
    // pager.pagination({
    //     buttons:"#tb"
    // });
});

function statusFormatter(value,record,index) {
    if(value == 1){
        return "待审核";
    }
    if(value == 2){
        return "提交上级审核";
    }
    if(value == 3){
        return "待批改";
    }
    if(value == 4){
        return "待缴费";
    }
    if(value == 5){
        return "已缴费";
    }
    if(value == 6){
        return "已作废";
    }
    if(value == 7){
        return "已修改待审核";
    }
}